import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import swal, {SweetAlertType} from 'sweetalert2';
import {HttpService} from "@core/services/http.service";

@Component({
    selector: 'app-logic',
    templateUrl: './logic.component.html',
    styleUrls: ['./logic.component.less']
})
export class LogicComponent implements OnInit {
    @Input()
    node_id = [];

    @Input()
    gids = [];

    @Input()
    nextNodes = [];

    @Input()
    n_name;

    @Input()
    p_id;

    @Input()
    typename;

    @Output() toggleEvent = new EventEmitter<any>();
    @Output() saveAllLogic = new EventEmitter<any>();
    @Output() saveAllMultiLogic = new EventEmitter<any>();

    options = [
        {value: 'and', label: '全部'},
        {value: 'or', label: '任何'}
    ];
    logicType = this.options[0];

    targets = [

    ];

    target;

    scenes = [
    ];

    condition = [
        {value: 'review', label: '评论数'},
        {value: 'select', label: '选项值'},
    ];

    operation = [
        {value: '=', label: '='},
        {value: '≠', label: '≠'},
        {value: '>', label: '>'},
        {value: '≥', label: '≥'},
        {value: '<', label: '<'},
        {value: '≤', label: '≤'},
        {value: '≤', label: '≤'},
        {value: 'include', label: '包含'},
        {value: 'exclude', label: '不包含'},
    ];

    selectOperation = [
        {value: '=', label: '='},
        {value: '≠', label: '≠'},
    ];


    results = [
        {value: 'A', label: 'A'},
        {value: 'B', label: 'B'},
        {value: 'C', label: 'C'},
        {value: 'D', label: 'D'},
        {value: 'E', label: 'E'},
        {value: 'F', label: 'F'},
        {value: 'G', label: 'G'},
        {value: 'H', label: 'H'},
        {value: 'I', label: 'I'},
        {value: 'G', label: 'G'},
        {value: 'K', label: 'K'},
        {value: 'L', label: 'L'},
        {value: 'M', label: 'M'},
        {value: 'N', label: 'N'},
        {value: 'O', label: 'O'},
        {value: 'P', label: 'P'},
        {value: 'Q', label: 'Q'},
        {value: 'R', label: 'R'},
        {value: 'S', label: 'S'},
        {value: 'T', label: 'T'},
        {value: 'U', label: 'U'},
        {value: 'V', label: 'V'},
        {value: 'W', label: 'W'},
        {value: 'X', label: 'X'},
        {value: 'Y', label: 'Y'},
        {value: 'Z', label: 'Z'},
    ];

    singleLogicData = [];

    allLogicData = [];

    @Input()
    set inLogic(value) {
        if (value) {
            for (const data of value){
                if (data.logicType !== 'default') {
                    this.allLogicData.push(data);
                }
            }
        }
    }

    moreLogic = false;
    toggleLogic() {
        this.moreLogic = !this.moreLogic;
    }



    constructor(public http: HttpService) {
    }

    ngOnInit() {

        for(let node of this.node_id){
            this.targets.push(node);
        }
        for(let group of this.gids){
            this.scenes.push(group)
        }
        if(this.targets.length === 0) {
            this.target = {};
        } else{
            this.target = this.targets[0];
        }

        this.getProcess();
        // console.log(this.targets)
    }



    addLand() {
        this.pushObj();
    }

    pushObj() {
        this.singleLogicData.push({
            logicScene: this.scenes[0],
            logicCondition: this.condition[0],
            logicOperation: this.operation[0],
            logicResultSelect: {value: ''},
            logicResultNumber: 0
        });
    }

    conditionChange(e, i) {
        if (!e ) {
            if (this.singleLogicData[i].logicCondition.value === 'select' ) {
                this.singleLogicData[i] = {
                    logicScene: this.scenes[0],
                    logicCondition: this.condition[1],
                    logicOperation: this.selectOperation[0],
                    logicResultSelect: this.results[0],
                    logicResultNumber: ''
                };
            } else {
                this.singleLogicData[i] = {
                    logicScene: this.scenes[0],
                    logicCondition: this.condition[0],
                    logicOperation: this.operation[0],
                    logicResultSelect: {value: ''},
                    logicResultNumber: 0
                };
            }

        }
        return true;
    }

    delete(i) {
        this.singleLogicData.splice(i, 1);
    }

    deleteAll() {
        this.singleLogicData.length = 0;
    }

    save() {

        if (this.singleLogicData.length === 0) {
            return false;
        }

        const obj = {
            logicType: this.logicType.value,
            rules: [],
            target: this.target.value,
            label: this.target.label
        }
        for (const data of this.singleLogicData) {
            obj.rules.push({
                logicScene: data.logicScene.value,
                logicCondition: data.logicCondition.value,
                logicOperation: data.logicOperation.value,
                logicResultSelect: data.logicResultSelect.value || '',
                logicResultNumber: data.logicResultNumber || ''
            });
        }
        this.allLogicData.push(obj);
        this.singleLogicData.length = 0;
        console.log(this.allLogicData);
    }

    deleteRule(i) {
        this.allLogicData.splice(i, 1);
    }

    clearAll() {
        this.allLogicData.length = 0;
    }

    toggle() {
        this.toggleEvent.emit();
    }

    saveAll() {
        // if (this.allLogicData.length === 0) {
        //     this.saveAllLogic.emit([{
        //         logicType: 'default',
        //         rules: [],
        //         target: this.target.value
        //     }]);
        // } else {
        //     this.saveAllLogic.emit(this.allLogicData);
        // }
        // swal(`逻辑保存成功`, '', 'info');
    }

    show = false;

    change($event) {
        if (!$event) {
            if (this.logicType.value !== 'default') {
                this.show = true;
            } else {
                this.show = false;
            }
        }
    }


    // 序列分组和分组内的节点

    processList = [];

    getProcess () {
        this.http.getProjProcess({p_id: this.p_id, isCloud: this.typename}).subscribe(res => {
            if (res.list ) {
                this.processSerilize(JSON.parse(res.list[0].p_data));
            }
        });
    }

    processSerilize(arr) {
        for (const value of arr) {
            if (value.type === 'group') {
                this.processList.push(value);
            }
        }
    }

    // 多场景逻辑

    @Input()
    set multiLogics(value) {
        if (value && value.length !== 0 && value[0].rules) {
            this.multiLogicData = value[0].rules;
            this.multiLogicType = value[0].logicType;
        }
    }

    multiLogicData = [];
    multiLogicType = this.options[0].value;

    addMoreLogic() {
        this.multiLogicData.push({
            firstGroup: this.processList[0],
            secondGroup: this.processList[0],
            firstScene: this.processList[0].nodes[0],
            secondScene: this.processList[0].nodes[0],
            firstCondition: this.condition[0].value,
            secondCondition: this.condition[0].value,
            logic: this.operation[0].value,
            targetGroup: this.processList[0],
            targetScene: this.processList[0].nodes[0]
        });
    }

    moreDelete(i) {
        this.multiLogicData.splice(i, 1);
    }

    moreDeleteAll() {
        this.multiLogicData.length = 0;
    }



}
